﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Xml;
using System.Text;

public partial class FrontEnd : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bool isauth = Request.IsAuthenticated;
        if (isauth == true)
        {
            LoginStatus.Visible = true;
            Nav_Top_Current.Visible = true;
        }
        else
        {
            LoginStatus.Visible = false;
        }
                
    }
    
    protected void left_nav_Login_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string uname = left_nav_Login.UserName.Trim(); //Get the username from the control
        string password = left_nav_Login.Password.Trim(); //get the Password from the control
        bool flag = AuthenticateUsername(uname, password);
        if (flag == true)
        {
            e.Authenticated = true;
            UpdateFields();
        } else {
            e.Authenticated = false;
        }
    }

    private void UpdateFields()
    {
        DisplayLoginName.Visible = true;
        LoginStatus.Visible = true;
    }

    private bool AuthenticateUsername(string uname, string password)
    {
        bool authed = false;
        string connString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        string selectState = "Select * from Users where ((Users.[User_ID] =" + uname + ") AND (Password ='" + password + "'))";
        //SELECT * FROM Users WHERE (((Users.[User_ID])=4) AND ((Users.[Password])="ABCabc"));

        DataSet userDS = new DataSet();
        OleDbConnection db_conn = new OleDbConnection(connString);
        db_conn.Open();
        OleDbDataAdapter db_dataAdapter = new OleDbDataAdapter(selectState, db_conn);
        db_dataAdapter.Fill(userDS);
        db_conn.Close();
        if (userDS != null)
        {
            if (userDS.Tables[0].Rows.Count > 0)
                authed = true;
        }

        return authed;
                
    }


    protected void Nav_Top_Current_Click(object sender, EventArgs e)
    {
        bool isauth = Request.IsAuthenticated;
        if (isauth == true)
        {
            Response.Redirect(Incident_Page_to_Show(Page.User.Identity.Name));
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private string Incident_Page_to_Show(string loggedinAs)
    {
        string postBack = "";
        int state = 0;
        string organisation = "";

        string uname = loggedinAs;
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        //string selectState = "Select * from Users where (Users.[User_ID] =" + uname + ")";
        //"SELECT Users.*, Organisation.State, Organisation.Org_ID, Teams.Team_ID FROM (Organisation INNER JOIN Teams ON Organisation.Org_ID = Teams.[Organisation Name]) INNER JOIN Users ON Teams.Team_ID = Users.Team_ID where (Users.[User_ID] =" + uname + ")";
        string selectState = "SELECT Users.User_ID, Organisation.Organisation_Name, Organisation.State FROM Organisation INNER JOIN Users ON Organisation.Org_ID = Users.Organisation_Name where (Users.[User_ID] =" + uname + ")";


        DataSet userDS = new DataSet();
        OleDbConnection db_conn = new OleDbConnection(connString);
        db_conn.Open();
        OleDbDataAdapter db_dataAdapter = new OleDbDataAdapter(selectState, db_conn);
        db_dataAdapter.Fill(userDS);
        db_conn.Close();
        if (userDS != null)
        {

            //int teamID = Int32.Parse(userDS.Tables[0].Rows[0].ItemArray[1].ToString());
            //int orgID = Int32.Parse(userDS.Tables[0].Rows[0].ItemArray[0].ToString());

            state = Int32.Parse((userDS.Tables[0].Rows[0].ItemArray[2].ToString()));
            organisation = userDS.Tables[0].Rows[0].ItemArray[1].ToString();


            if (organisation.Equals("DMS")) // DMS
            {
                postBack = "~/DMS.aspx";
            } 
                 
            else if (organisation.Equals("Parliament")) // Parl
            {
                postBack = "~/State Official Search.aspx?state_ID=" + state;
            }

            else if (String.Equals(organisation, "Public") == false) //Dept
            {
                postBack = "~/Department Administrator Page.aspx?state_ID=" + state +"&dept="+organisation;
            }
        }
        return postBack;
    }

    protected void Nav_Top_Submission_Click(object sender, EventArgs e)
    {
        bool isauth = Request.IsAuthenticated;
        if (isauth == true)
        {
            Response.Redirect("~/create_new.aspx?user_id=" + 4);
        }
        else
        {
            Response.Redirect("~/create_new.aspx?user_id=" + 7);
        } 
    }
}



